package com.fg.pro.trade.service.impl;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fg.pro.model.trade.dto.GmUnitDto;
import com.fg.pro.trade.dao.TradeUnitDao;
import com.fg.pro.trade.entity.TradeGoods;
import com.fg.pro.trade.entity.TradeUnit;
import com.fg.pro.trade.service.TradeUnitService;
import com.fg.pro.trade.utils.GmHttpUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import static com.fg.pro.trade.utils.GmRequestUrl.GM_UNIT_URL;
import static com.fg.pro.trade.utils.GmRequestUrl.REDIS_LOCK_URL;

/**
 * 单位(TradeUnit)表服务实现类
 *
 * @author makejava
 * @since 2025-08-13 09:37:31
 */
@Service("tradeUnitService")
public class TradeUnitServiceImpl extends ServiceImpl<TradeUnitDao, TradeUnit> implements TradeUnitService {

    @Autowired
    GmHttpUtils gmHttpUtils;
    @Autowired
    RedissonClient redissonClient;
    @Override
    public PageInfo getPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<TradeUnit> list = query().list();
        PageInfo<TradeUnit> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public boolean refresh(Date date) throws InterruptedException {
        RLock lock = redissonClient.getLock(REDIS_LOCK_URL+GM_UNIT_URL);
        boolean tryLock = lock.tryLock();
        if(!tryLock){
            return false;
        }
        try {

            return refreshTradeUnitByLock(date);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            lock.unlock();
        }
    }

    @Transactional
    public boolean refreshTradeUnitByLock(Date date) throws InterruptedException {
        GmUnitDto.GmUnitReponse gmUnitReponse = gmHttpUtils.httpPostByToken(GM_UNIT_URL, new GmUnitDto().new GmUnitRequest().gmUnitRequest(999), GmUnitDto.GmUnitReponse.class);
        LinkedList<TradeUnit> newList = new LinkedList<>();
        for (GmUnitDto.GmUnitReponse.UnitData unit : gmUnitReponse.getUnits()) {
            TradeUnit tradeUnit = new TradeUnit();
            tradeUnit.setId(unit.getUnit_id());
            tradeUnit.setUnitGroupId(unit.getGroup_id());
            tradeUnit.setCreateTime(new Date(ObjUtil.isEmpty(unit.getCreate_time())?0:unit.getCreate_time()));
            tradeUnit.setUpdateTime(new Date(ObjUtil.isEmpty(unit.getUpdate_time())?0:unit.getUpdate_time()));
            tradeUnit.setUnitName(unit.getName());
            tradeUnit.setUnitType(unit.getType());
            tradeUnit.setUnitRate(unit.getRate());
            tradeUnit.setUnitParentId(unit.getParent_id());
            newList.add(tradeUnit);
        }
        boolean save = saveOrUpdateBatch(newList);
        return save;
    }
}

